<?php

declare(strict_types=1);

namespace app\admin\command;

use app\common\model\Matches;
use app\common\model\MatchesLiveConfig;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class MatchesConfig extends Command
{
    //对于赛事的配置   文字直播，天气，积分对比 赛事数据保存后执行此定时器
    //时间条件暂时未加
    protected function configure()
    {
        // 指令配置
        $this->setName('m_config')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->setDescription('赛事配置，文字直播，天气，积分对比等');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = trim($input->getArgument('action'));
        $start = strtotime(date('Y-m-d', time()) . '00:00:00');
        $end = strtotime(date('Y-m-d', time()) . '23:59:59');
        Matches::field('id,api_id,match_id,home_team_name as name,type')->where(['type' => $action])->whereTime('match_time', 'between', [$start, $end])->chunk(100, function ($matches) use ($action, $output) {
            foreach ($matches as $key => $matche) {
                $output->writeln('=========================循环第' . ($key + 1) . '个数据，开始循环赛事名称为:' . $matche['name'] . '=====================================');
                // 处理结果集...
                if ($action == 'football') {
                    $match_id = $matche['match_id'];
                } else {
                    $match_id = $matche['api_id'];
                }
                $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/match/process?match_id=' . $match_id;
                $res = curl_get($url);
                $arr = json_decode($res, true);
                if ($arr['code'] == 0 && count($arr['data']) > 0) {
                    if (array_key_exists('environment', $arr['data']) && !empty($arr['data']['environment'])) {
                        $environment = json_encode($arr['data']['environment'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $environment = NULL;
                    }
                    if (array_key_exists('incidents', $arr['data']) && !empty($arr['data']['incidents'])) {
                        $incidents = json_encode($arr['data']['incidents'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $incidents = NULL;
                    }
                    if (array_key_exists('processes', $arr['data']) && !empty($arr['data']['processes'])) {
                        $processes = json_encode($arr['data']['processes'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $processes = NULL;
                    }
                    if (array_key_exists('stats', $arr['data']) && !empty($arr['data']['stats'])) {
                        $stats = json_encode($arr['data']['stats'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $stats = NULL;
                    }
                    if (array_key_exists('tlive', $arr['data']) && !empty($arr['data']['tlive'])) {
                        $tlive = json_encode($arr['data']['tlive'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $tlive = NULL;
                    }
                    if (array_key_exists('trend', $arr['data']) && !empty($arr['data']['trend'])) {
                        $trend = json_encode($arr['data']['trend'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                    } else {
                        $trend = NULL;
                    }
                    $result_arr = [
                        'environment' => $environment,
                        'incidents' => $incidents,
                        'processes' => $processes,
                        'stats' => $stats,
                        'tlive' => $tlive,
                        'trend' => $trend,
                        'match_id' => $match_id,
                        'foreign_key_id' => $matche->id,
                    ];
                    $is_find = MatchesLiveConfig::where(['foreign_key_id' => $matche->id])->find();
                    if (empty($is_find)) {
                        MatchesLiveConfig::create($result_arr);
                        $str = 'add';
                    } else {
                        //更新
                        $is_find->save($result_arr);
                        $str = 'update';
                    }
                    $output->writeln('=========================' . $str . '完成=====================================');
                } else {
                    $output->writeln('api参数出错');
                }
            }
        });
        // 指令输出
        $output->writeln('结束');
    }
}
